<?php


class Login extends MY_Controller {
	
	// group names
	private $HotGroup = "hotel";
	private $SchGroup = "school";
	
	
	
	
	function Login() {
		parent::__construct();
		
		$this->load->helper(array('form'));
		
		$this->load->library(array('carabiner'));
	}
	
	
	
	
	
	/**
	 * check if someone is loged in, if so ... redirect them accordingly
	 */
	private function check_logon_users() {
		
		if ($this->ion_auth->is_admin()) {								// Admin
			redirect('Admin/index');
		}
		else { 
			if ($this->ion_auth->is_group($this->SchGroup)) { 			// School
				redirect('School/index');
			}
			else if($this->ion_auth->is_group($this->HotGroup)) {		// Hotel
				redirect('Hotel/index');
			}
		}
	}
	
	
	
	
	/**
	 * Entry point
	 */
	public function index() {
		
		// check if someone is already logged in and redirect them if so ...
		$this->check_logon_users();
			
		// only enter here if the button submit is pressed
		if($this->input->post('login')) {
			
			// set form rules
			$this->form_validation->set_rules('user_name','user',		'required');
			$this->form_validation->set_rules('user_pass','password', 	'required');
			
			// define error delimiters so these can be customized with css styles
			$this->form_validation->set_error_delimiters('<p class="error">','</p>');
				
			// see if the previous rules are ok ... 
			if ($this->form_validation->run()) {$this->firephp->log('required fields ok ... ');
				if ($_POST) {
					$user = $this->input->post('user_name', true);
					$pass = $this->input->post('user_pass', true);
					
					// log in ... 
					if ($this->ion_auth->login($user,$pass,false)) {
						if ($this->ion_auth->is_admin())
							redirect("Admin/index");
						else if($this->ion_auth->is_group($this->HotGroup)) {
							$this->session->set_userdata('item','value');
							redirect("Hotel/index");
						}
						else if($this->ion_auth->is_group($this->SchGroup))
							redirect("School/index");
						else {
							// it should never reach this point ... but if he did show login form again
							$this->showForm();
						}
					}
					else {	// ... login attempt failed
						$this->showForm();
					}
				}
			}
			else {	// ... the rules dont apply? show login form again ...
				$this->showForm();
			}
		}
		else {		// ... submite button not pressed? display the login form again ...
			$this->showForm();
		}			
	}
	
	
	
	/**
	 * Show login form
	 */
	private function showForm() {
		
		$this->load->view('public/loginForm');
		
		if (false) {
			$this->load->view('public/login_head');
			$this->load->view('public/login_main');
			$this->load->view('public/login_footer');
		}
	}
	
	
	
	
}



